if __name__ == "__main__":
    # 读取文件
    i = 0
    j = 0
    with open("demo.txt", "r", encoding="utf-8") as f:
        dic = {}
        timeDic = {}
        # 逐行读取
        for line in f:
            # 去除换行符
            line = line.strip()
            dic[i] = line
            if line.startswith("active sync time -> current time"):
                timeDic[j] = dic[i - 2] or dic[i - 1]
                j += 1
            # print(f"第{i}行：{line}")
            i += 1

    # 打印字典
    # print(timeDic)

    # 遍历字典
    dic = {}
    for key, value in timeDic.items():
        # 打印键和值
        # print(f"第{key}个时间：{value}")
        # 截取[]之间的内容
        start = value.find("[")
        end = value.find("]")
        time = value[start + 1 : end]
        # 截取 2025-03-19 13:37:13.251 中的 251
        millisecond = time.split(".")[1]
        dic[key] = [time, millisecond]
        # 计算前一个millisecond和当前millisecond的差值
        if False:
            # 计算差值
            diff = int(millisecond) - int(timeDic[key - 1])
            if diff < 0:
                diff += 1000
            print(f"第{key}个时间和第{key - 1}个时间的差值：{diff}")

    # 打印字典
    for key, value in dic.items():
        # print(f"第{key}个时间：{value}")
        millisecond = value[1]
        diff = 0
        # 计算差值
        if key > 0:
            diff = int(millisecond) - int(dic[key - 1][1])
            if diff < 0:
                diff += 1000
        print(f"time-> {value[0]}, diff：{diff}")
        # 写入文件中
        with open("diff.txt", "a", encoding="utf-8") as f:
            f.write(f"time-> {value[0]}, diff：{diff}\n")
